<!--
 * @Author: 赵克强 huohuo625@163.com
 * @Date: 2023-11-07 16:24:00
 * @LastEditors: 赵克强 huohuo625@163.com
 * @LastEditTime: 2025-04-11 10:16:38
 * @FilePath: \hy-ui\src\views\render\components\operateModal.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="operateModal">
    <el-dialog
      :title="
        tableColumns.customPopupTitle ? tableColumns.customPopupTitle : title
      "
      :visible.sync="open"
      width="80%"
      :before-close="close"
      custom-class="renderDialog"
    >
      <div v-loading="modelLoading">
        <v-form-render ref="vFormRef" :option-data="optionData">
        </v-form-render>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button
          type="primary"
          @click="handleSubmit()"
          v-if="modalConfig.itemConfig.buttonType !== 'handleDetail'"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { saveOrUpdate } from "@/api/render/pager.js";
import request from "@/utils/request";
export default {
  name: "HyUiOperateModal",
  props: {
    tableColumns: {
      type: Object,
      default: () => {},
    },
    getList: {
      type: Function,
      default: () => {},
    },
    open: {
      type: Boolean,
      default: false,
    },
    close: {
      type: Function,
      default: () => {},
    },
    title: {
      type: String,
      default: "提示",
    },
    modelLoading: {
      type: Boolean,
      default: false,
    },
    modalConfig: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      optionData: {},
    };
  },
  watch: {
    modalConfig: {
      immediate: true,
      async handler() {
        const _this = this;
        this.$nextTick(() => {
          if (_this.modalConfig) {
            let formJson = {
              formConfig: _this.modalConfig?.formConfig,
              widgetList: _this.modalConfig?.widgetList,
            };
            if (_this.modalConfig?.dictList) {
              _this.optionData = _this.modalConfig?.dictList;
            }
            _this.$refs.vFormRef.setFormJson(formJson);
            this.$nextTick(() => {
              if (this.modalConfig?.detailData) {
                this.$refs.vFormRef.setFormData(this.modalConfig?.detailData);
              }
              if (this.modalConfig.itemConfig.buttonType === "handleDetail") {
                this.$refs.vFormRef.disableForm();
              }
            });
          }
        });
      },
    },
  },
  mounted() {},

  methods: {
    async handleSubmit() {
      if (this.modalConfig.formConfig?.onFormSaveBefore) {
        new Function(this.modalConfig.formConfig?.onFormSaveBefore)();
      }
      const oldValue = this.modalConfig?.detailData
      this.$refs.vFormRef.validateForm(async (valid) => {
        if (valid) {
          const params = await this.$refs.vFormRef.getFormData();
          if (this.modalConfig?.detailData) {
            params.id = this.modalConfig?.detailData?.id;
          }
          if (this.modalConfig.formConfig.erMark) {
            params.erMark = this.modalConfig.formConfig.erMark;
          }
          const submit = async () => {
            const res = await saveOrUpdate(this.modalConfig?.tableName, params);
            if (res.code === 200) {
              if (this.modalConfig.formConfig?.onFormSaveAfter) {
                new Function(this.modalConfig.formConfig?.onFormSaveAfter)();
              }
              if (this.modalConfig.formConfig.afterAddForJava && this.modalConfig.itemConfig.buttonType === 'handleAdd') {
                await request({
                  url: this.modalConfig.formConfig.afterAddForJava,
                  method: "post",
                  data:res?.data,
                });
              }
              if (this.modalConfig.formConfig.afterUpdateForJava && this.modalConfig.itemConfig.buttonType === 'handleEdit') {
                await request({
                  url: this.modalConfig.formConfig.afterUpdateForJava,
                  method: "post",
                  data:{
                    oldValue,
                    newValue:params
                  },
                });
              }
              this.$message.success(`${this.title}成功`);
              this.close();
              this.getList();
            }
          };
          if (this.modalConfig.formConfig.beforeAddForJava && this.modalConfig.itemConfig.buttonType === 'handleAdd') {
            const res = await request({
              url: this.modalConfig.formConfig.beforeAddForJava,
              method: "post",
              data:params,
            });
            if (res.code === 200) {
              await submit();
            }
          }else if (this.modalConfig.formConfig.beforeUpdateForJava && this.modalConfig.itemConfig.buttonType === 'handleEdit') {
            const res = await request({
              url: this.modalConfig.formConfig.beforeUpdateForJava,
              method: "post",
              data:{
                    oldValue,
                    newValue:params
                  },
            });
            if (res.code === 200) {
              await submit();
            }
          }else{
            submit()
          }
          
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.operateModal {
  ::v-deep .el-form-item__error {
    z-index: 5;
  }
  ::v-deep .renderDialog {
    &>.el-dialog__body {
      padding-top: 0;
    }
  }
}
</style>